/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cmb.chat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import org.apache.log4j.Logger;

/**
 *
 * @author jameseaton
 */
public class Client {

    private int clientId;
    private String clientName;
    private Socket socket;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private Logger logger;
    private MessageReceiver msgReceiver;
    private boolean running = true;


    {
        logger = Logger.getLogger(Client.class);
    }

    public Client(Socket socket, MessageCallback msgCallback) throws IOException
    {
        this.socket = socket;
        logger.info("Initialising Client");
        try
        {
            
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            
        }
        catch (IOException e1)
        {
            try{
                socket.close();
                throw e1;
            }
            catch (IOException e2)
            {
                throw e2;
            }
        }

        msgReceiver = new MessageReceiver(msgCallback);
       
    }



    private class MessageReceiver implements Runnable {

        MessageCallback msgCallback;

        public MessageReceiver(MessageCallback msgCallback)
        {
            this.msgCallback = msgCallback;
        }


        public void run(){

            while (running)
            {
                try
                {
                    Message message = (Message) ois.readObject();
                    msgCallback.receiveMessage(message);
                }
                catch (ClassNotFoundException ce)
                {
                    logger.error("Class not found exception thrown " + ce.getMessage());
                }
                catch (IOException ce)
                {
                    logger.info("Socket is closed");
                    break;
                }
            }
        }


    }




    public void sendMessage(Message message)
    {
       
       logger.info("Running ClientHandler for socket " + socket.getLocalPort());

            try
            {
              //  ois = new ObjectInputStream(socket.getInputStream());
                oos.writeObject(message);
                logger.info("The following message has been transmitted sent by client"
                             + getClientId() + " " + message.getMessage());
              
            }

            catch (IOException ce)
            {
                logger.error("Error sending message " + ce);
            }

        

    }

    public void close(){
        try
        {
            running = false;
            Thread.currentThread().sleep(5000);
            oos.close();
            ois.close();

        }
        catch (IOException io){
            logger.error("IO Error while shutting down client " + io);
        }
        catch (InterruptedException ie){}
    }

    /**
     * @return the clientId
     */
    public int getClientId() {
        return clientId;
    }

    /**
     * @param clientId the clientId to set
     */
    public void setClientId(int clientId) {
        this.clientId = clientId;
    }

    /**
     * @return the clientName
     */
    public String getClientName() {
        return clientName;
    }

    /**
     * @param clientName the clientName to set
     */
    public void setClientName(String clientName) {
        this.clientName = clientName;
    }


}
